package mao.thinkinjava.io;

import java.io.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * 不使用的文件要及时关闭。
 */
public class GZIPcompress {
    public static void main (String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new FileReader("text.txt"));
        int i;
        BufferedOutputStream out = new BufferedOutputStream(
                new GZIPOutputStream(new FileOutputStream("test.gz")));
        while ((i = in.read()) != -1){
            System.out.print((char)i);
            out.write(i);
        }
        //使用完文件要及时关闭，变不必要的饿麻烦。
        in.close();
        out.close();

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                new GZIPInputStream(new FileInputStream("test.gz"))
        ));
        String s ;
        while ((s = reader.readLine()) != null){
            System.out.println(s);
        }
        reader.close();
    }
}
